package org.codefinger.dao.callback;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.codefinger.dao.impl.ResultSetCallback;
import org.codefinger.dao.util.jdbc.JDBCUtil;

public class MapSetQueryCallback implements ResultSetCallback<Set<Map<String, Object>>> {

	public static final MapSetQueryCallback	INSTANCE	= new MapSetQueryCallback();

	private MapSetQueryCallback() {
		super();
	}

	@Override
	public Set<Map<String, Object>> getResult(ResultSet resultSet, String[] aliases) throws SQLException {
		Set<Map<String, Object>> set = new HashSet<Map<String, Object>>();
		Map<String, Object> map;
		ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
		int i;
		while (resultSet.next()) {
			i = 1;
			map = new HashMap<String, Object>();
			for (String alias : aliases) {
				map.put(alias, JDBCUtil.getResult(//
						resultSet, i, resultSetMetaData.getColumnType(i++)));
			}
			set.add(map);
		}
		return set;
	}

}
